﻿Namespace EIBData.EIS
    Public NotInheritable Class EIBDataEIS_15
        Inherits EIBDataEIS_Base

        Protected _value As String


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data

            Dim valueBase As Byte() = New Byte(13) {}
            For i = 0 To 13
                valueBase(i) = data(i + 1)
            Next
            _value = System.Text.Encoding.UTF8.GetChars(valueBase)
            _value = _value.Replace(Chr(0), "")
        End Sub

        Sub New(value As String)
            If value.Length > 14 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _value = value
            Dim valueArr As Byte() = System.Text.Encoding.UTF8.GetBytes(value)
            Dim valueBase As Byte() = New Byte(13) {}
            Array.Copy(valueArr, valueBase, valueArr.Length)

            MyBase._data = New Byte() {0,
                                      valueBase(0),
                                      valueBase(1),
                                      valueBase(2),
                                      valueBase(3),
                                      valueBase(4),
                                      valueBase(5),
                                      valueBase(6),
                                      valueBase(7),
                                      valueBase(8),
                                      valueBase(9),
                                      valueBase(10),
                                      valueBase(11),
                                      valueBase(12),
                                      valueBase(13)
                                     }
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = EIS_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return _value
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_value}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(String)}
            End Get
        End Property

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", EIS_Name))
            list.Add(New EIB.Info("Value", _value))

            Return list
        End Function

        Public Shared Shadows ReadOnly Property EIS_OctetLength As Byte
            Get
                Return 15
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_Name As String
            Get
                Return "EIS 15"
            End Get
        End Property


        Public ReadOnly Property Value As String
            Get
                Return _value
            End Get
        End Property
    End Class
End Namespace